package com.orange.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.orange.util.AppConfig;
import com.orange.util.ReturnCode;
import com.orange.util.UploadUtil;

/**
 * 文件上传
 * @author bcb
 *
 */
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController{

	static final Log logger = LogFactory.getLog(UploadController.class);
	
	@Autowired
	private UploadUtil uploadUtil;
	
	@Autowired
	private AppConfig appconfig;
	
	/**
	 * 学校文件解析
	 * @param Filedata
	 * @return
	 */
	@RequestMapping(value="/file" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> uploadFile(
			@RequestParam("file")MultipartFile Filedata
			){
		try {
			String str=uploadUtil.uploadFile(Filedata);
			if("22".equals(str)){
				return toResponse(null,ReturnCode.FILE_TYPE_NOT_MATCH,"文件类型有误");
			}
			return toResponse(str,ReturnCode.SUCCESS,null);
		} catch (Exception e) {
			logger.error("文件上传失败", e);
			return toResponse(null,ReturnCode.FAIL,"文件上传失败");
		}
	}

	/**
	 * 图片上传
	 * @param Filedata
	 * @return
	 */
	@RequestMapping(value="/pic" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> uploadPhoto(
			@RequestParam("pic")MultipartFile Filedata
			){
		try {
			if(Filedata.getSize()/1024>appconfig.getPicSize()*1024){
				return toResponse(null,ReturnCode.FILE_TYPE_NOT_MATCH,"图片大小超出上限");
			}
			String str=uploadUtil.uploadpic(Filedata);
			if("22".equals(str)){
				return toResponse(null,ReturnCode.FILE_TYPE_NOT_MATCH,"文件类型有误");
			}
			return toResponse(str,ReturnCode.SUCCESS,null);
		} catch (Exception e) {
			logger.error("文件上传失败", e);
			return toResponse(null,ReturnCode.FAIL,"图片上传失败");
		}
	}
	
}
